﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NetDiskPRJ.DBUtility;
using System.Data.SqlClient;
using System.Data;

namespace NetDiskPRJ.DAL
{
    public class Apslogrec
    {
        public Apslogrec()
        {
 
        }

        /// <summary>
        /// 增加日志
        /// <creator>wangl</creator>
        /// <date>2012-01-05</date> 
        /// </summary>
        /// <param name="?"></param>
        /// <returns></returns>
        public bool Add(Model.Apslogrec model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" insert into t_apslogrec (start_time,over_time,logname,operator,ip_addr,computername,FEntityNo)");
            //strSql.Append("values(@start_time,@over_time,@logname,@operator,@ls_ipaddr, @ls_computername) ;");
            // modify by wangzhubao 2012/06/13
            strSql.Append("values(@start_time,getdate(),@logname,@operator,@ls_ipaddr, @ls_computername,@FEntityNo) ;");
            SqlParameter[] parameters = {
                    new SqlParameter("@start_time", SqlDbType.DateTime),
                    new SqlParameter("@over_time", SqlDbType.DateTime),
                    new SqlParameter("@logname", SqlDbType.Char,8),
                    new SqlParameter("@operator", SqlDbType.VarChar,60),
                    new SqlParameter("@ls_ipaddr", SqlDbType.VarChar,20),
                    new SqlParameter("@ls_computername", SqlDbType.VarChar,255),
                    new SqlParameter("@FEntityNo", SqlDbType.NVarChar,30)
                    
                                        };
            parameters[0].Value = model.start_time;
            parameters[1].Value = model.over_time;
            parameters[2].Value = model.logname;
            parameters[3].Value = model.Operator;
            parameters[4].Value = model.ip_addr;
            parameters[5].Value = model.computername;
            parameters[6].Value = model.FEntityNo;
            int i = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (i != 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// <creator>sunlj</creator>
        /// <date>2013-02-19</date> 
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        /// <returns></returns>
        public List<Model.Apslogrec> GetList(string top, string strWhere, string Sort)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (top != "")
            {
                strSql.Append(" top " + top);
            }
            strSql.Append(" seq_id,start_time,over_time,logname,operator,ip_addr,computername ");
            strSql.Append(" FROM t_apslogrec");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            if (Sort != "")
            {
                strSql.Append(Sort);
            }

            return DataTableToList(DbHelperSQL.Query(strSql.ToString()).Tables[0]);
        }

        /// <summary>
        /// 获得数据MOD集合
        /// <creator>zhongyl</creator>
        /// <date>2011-12-30</date> 
        /// </summary>
        /// <param name="DT">数据列</param>
        /// <returns></returns>
        public List<NetDiskPRJ.Model.Apslogrec> DataTableToList(DataTable dt)
        {
            List<NetDiskPRJ.Model.Apslogrec> modelList = new List<NetDiskPRJ.Model.Apslogrec>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                NetDiskPRJ.Model.Apslogrec model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new NetDiskPRJ.Model.Apslogrec();
                    if (dt.Rows[n]["seq_id"] != null && dt.Rows[n]["seq_id"].ToString() != "")
                    {
                        model.seq_id = decimal.Parse(dt.Rows[n]["seq_id"].ToString());
                    }
                    if (dt.Rows[n]["start_time"] != null && dt.Rows[n]["start_time"].ToString() != "")
                    {
                        model.start_time = DateTime.Parse(dt.Rows[n]["start_time"].ToString());
                    }
                    if (dt.Rows[n]["over_time"] != null && dt.Rows[n]["over_time"].ToString() != "")
                    {
                        model.over_time = DateTime.Parse(dt.Rows[n]["over_time"].ToString());
                    }
                    if (dt.Rows[n]["logname"] != null && dt.Rows[n]["logname"].ToString() != "")
                    {
                        model.logname = dt.Rows[n]["logname"].ToString();
                    }
                    if (dt.Rows[n]["operator"] != null && dt.Rows[n]["operator"].ToString() != "")
                    {
                        model.Operator = dt.Rows[n]["operator"].ToString();
                    }
                    if (dt.Rows[n]["ip_addr"] != null && dt.Rows[n]["ip_addr"].ToString() != "")
                    {
                        model.ip_addr = dt.Rows[n]["ip_addr"].ToString();
                    }
                    if (dt.Rows[n]["computername"] != null && dt.Rows[n]["computername"].ToString() != "")
                    {
                        model.computername = dt.Rows[n]["computername"].ToString();
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

    }
}
